define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'connection/index' + location.search,
                    add_url: 'connection/add',
                    edit_url: 'connection/edit',
                    del_url: 'connection/del',
                    multi_url: 'connection/multi',
                    import_url: 'connection/import',
                    table: 'connection',
                }
            });

            var table = $("#table");

            table.on('load-success.bs.table', function (e, data) {
                $('label[for="type_data"]').parent().css('display','none'); // 隐藏客户类型搜索栏
                if(data.key==1){
                    $('label[for="status"]').parent().css('display','none');
                    $('label[for="report_time"]').parent().css('display','none');
                    $('label[for="type"]').parent().css('display','none');
                    $('label[for="staff.name"]').parent().css('display','none');
                    $('label[for="bind_staff.mobile"]').parent().css('display','none');
                    $('label[for="bindStaff.name"]').parent().css('display','none');
                    $('label[for="bindStaff.mobile"]').parent().css('display','none');
                    $('label[for="phone_two"]').parent().css('display','none');
                    table.bootstrapTable('hideColumn','status'); //可以隐藏某一个列
                    table.bootstrapTable('hideColumn','report_time'); //可以隐藏某一个列
                    table.bootstrapTable('hideColumn','type'); //可以隐藏某一个列
                    table.bootstrapTable('hideColumn','staff.name'); //可以隐藏某一个列
                    table.bootstrapTable('hideColumn','bind_staff.name'); //可以隐藏某一个列
                    table.bootstrapTable('hideColumn','bindStaff.name'); //可以隐藏某一个列
                    table.bootstrapTable('hideColumn','bindStaff.mobile'); //可以隐藏某一个列
                    table.bootstrapTable('hideColumn','phone_two'); //可以隐藏某一个列
                }else{
                    $('label[for="strike_data"]').parent().css('display','none');
                    table.bootstrapTable('hideColumn','strike_data'); //可以隐藏某一个列
                    table.bootstrapTable('hideColumn','nickname'); //可以隐藏某一个列
                    table.bootstrapTable('hideColumn','avatar'); //可以隐藏某一个列

                }
                $('label[for="canal_id"]').parent().css('display','none');

            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search:false,
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'type_data', title: __('Type_data'), searchList: {"0":__('Type_data 0'),"1":__('Type_data 1')},
                            formatter: Table.api.formatter.normal,visible:false},
                        /*{field: 'surname', title: __('Surname'), operate: 'LIKE'},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},*/
                        {field: 'nickname', title: __('Nickname'), operate: false},
                        {field: 'avatar', title: __('Avatar'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'moniker', title: __('Moniker'), operate: 'LIKE'},
                        {field: 'phone', title: __('Phone'), operate: '=',formatter: function (row,index) {
                                return index.phone_see
                            }},
                        //{field: 'phone_see', title: __('Phone'), operate: false},
                        {field: 'phone_two', title: __('Phone_two'), operate: '=',formatter: function (row,index) {
                                return index.phone_two_see
                            }},
                        {field: 'estimate_time', title: __('Estimate_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'status',title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3'),"4":__('Status 4')}, formatter: Table.api.formatter.status,},
                        {field: 'report_time', title: __('Report_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'type', title: __('Type'),operate: 'LIKE'},
                        //{field: 'type_id', title: __('Type_id')},
                        //{field: 'staff_id', title: __('Staff_id')},
                        {field: 'code', title: __('Code'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        //{field: 'strike_data', title: __('Strike_data'), searchList: {"0":__('Strike_data 0'),"1":__('Strike_data 1')}, formatter: Table.api.formatter.normal},
                        {field: 'strike_data',title: __('Strike_data'), searchList: {"0":__('strike_data 0'),"1":__('strike_data 1')}, formatter: Table.api.formatter.normal},
                        {field: 'strike_time', title: __('Strike_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        //{field: 'lapse_time', title: __('Lapse_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        //{field: 'connection_id', title: __('Connection_id')},
                        /*{field: 'collision_data', title: __('Collision_data'), searchList: {"0":__('Collision_data 0'),"1":__('Collision_data 1')}, formatter: Table.api.formatter.normal},
                        {field: 'collision_time', title: __('Collision_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        */
                        {field: 'staff_ibec.name', title: __('Staff_ibec.name'),operate: 'LIKE'},
                        {field: 'staff.name', title: __('Staff.name'),operate: 'LIKE'},
                        /*新增 start*/
                        {field: 'staff.mobile', title: __('Staff.mobile'),operate: 'LIKE'},
                        {field: 'ibec.name', title: __('Ibec.name'),operate: 'LIKE'},
                        /*新增 end*/
                        {field: 'bindStaff.name', title: __('BindStaff.name'),operate: 'LIKE',formatter: function (row,index) {
                                return index.bind_staff.name
                            }},
                        {field: 'bindStaff.mobile', title: __('BindStaff.mobile'),operate: 'LIKE',formatter: function (row,index) {
                                return index.bind_staff.mobile
                            }},
                        /*新增 start*/
                        {field: 'canal.name', title: __('Canal.name'),operate: 'LIKE'},
                        /*新增 end*/
                        {field: 'canal_id', title: __('Canal_id'),operate: "=",visible:false},
                        {field: 'operate', title: __('Operate'),
                            buttons: [
                                {
                                    name: 'see',
                                    title: __('查看'),
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    text: '查看',
                                    //icon: 'fa fa-folder-o',
                                    url: function(row,column){
                                        var url='connection/see?id='+row.id;
                                        return url;
                                    },
                                },
                                {
                                    name: 'ok',
                                    title:'已成交',
                                    text: '已成交',
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    icon: '',
                                    confirm: '确定改为已成交吗？',
                                    visible: function (row) {
                                        // console.log(row)
                                        //返回true时按钮显示,返回false隐藏
                                        if (row.status=='4' ) {
                                            return false
                                        }
                                        return true;

                                    },
                                    url: function (row, column) {
                                        var url = 'connection/operation?ids=' + row.id+'&status=4';
                                        return url;
                                    },
                                    success: function (data, ret) {
                                        table.bootstrapTable('refresh', {});
                                        return false;
                                    },
                                    error: function (data, ret) {
                                        top.Toastr.error(ret.msg);
                                        table.bootstrapTable('refresh', {});
                                        return false;
                                    },
                                },
                                {
                                    name: 'updateLog',
                                    title: __('UpdateLog'),
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    text: '绑定经纪人修改记录',
                                    //icon: 'fa fa-folder-o',
                                    url: function(row,column){
                                        var url='connection/updateLog?id='+row.id;
                                        return url;
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return row.bind_status;

                                    },
                                },
                            ]
                            , table: table, events: Table.api.events.operate,formatter:function (value, row, index){
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                that.table = table;
                                if(row.bind_staff_id==0){
                                    $(table).data("operate-edit",null);
                                }
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
$(".skip").click(function (data) {
    Fast.api.open(  $(this).attr("data-url"), $(this).attr("data-title"))
})
